package checkers.server.logic.classes;

import java.util.ArrayList;

import checkers.common.logic.classes.CheckersPosition;
import checkers.common.logic.classes.Point;

public class Game {
	
	private int gameId;
	private int[][] currentPositions;
	private Player playerOne;
	private Player playerTwo;
	private Player currentPlayer;
	private ArrayList<Point> lastMove;
	
	public Game(Player player) {
		this.gameId = Counter.getGameId();
		playerOne = player;
		lastMove = new ArrayList<>();
	}
	
	public int getId() {
		return gameId;
	}
	
	public boolean checkStart() {
		return (playerTwo != null);
	}
	
	public void addPlayer(Player playerTwo) {
		this.playerTwo = playerTwo;
	}
	
	public boolean checkMoves(Player player, ArrayList<Point> moves) {
		for (Point p : moves) {
			checkMove(player, p);
		}
		lastMove = moves;
		return true;
	}
	
	public void changePlayer() {
		if (currentPlayer == playerOne) {
			currentPlayer = playerTwo;
		} else {
			currentPlayer = playerOne;
		}
	}
	
	private boolean checkMove(Player player, Point move) {
		GameContext.getInstance().getLogger().addMessage("X: " + ((Integer)move.getX()).toString());
		GameContext.getInstance().getLogger().addMessage("Y: " + ((Integer)move.getY()).toString());
		return true;
	}

	public Player getPlayerOne() {
		return playerOne;
	}
	
	public void startGame() {
		if (isWhite(playerOne)) {
			setCurrentPlayer(playerOne);
		} else {
			setCurrentPlayer(playerTwo);
		}
		currentPositions = CheckersPosition.defaultPositions;
	}
	
	public boolean isWhite(Player player) {
		return player.getColor() == 0 ? false : true;
	}

	public Player getCurrentPlayer() {
		return currentPlayer;
	}

	public void setCurrentPlayer(Player currentPlayer) {
		this.currentPlayer = currentPlayer;
	}

	public ArrayList<Point> getLastMove() {
		return lastMove;
	}

	public void setLastMove(ArrayList<Point> lastMove) {
		this.lastMove = lastMove;
	}

	public int[][] getCurrentPositions() {
		return currentPositions;
	}

}
